package com.collect.tanzania.zhiScene.zhiWidget.zhiNavigationBar;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import androidx.fragment.app.FragmentActivity;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collect.tanzania.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ZHINavigationBar extends RelativeLayout implements View.OnClickListener {

    // 左边
    @BindView(R.id.ll_bar_left)
    LinearLayout ll_bar_left;
    // 左边的图片
    @BindView(R.id.iv_bar_left)
    ImageView iv_bar_left;
    // 左标题
    @BindView(R.id.tv_bar_left)
    TextView tv_bar_left;

    // 中间标题
    @BindView(R.id.tv_bar_title)
    TextView tv_bar_title;

    // 右边
    @BindView(R.id.ll_bar_right)
    LinearLayout ll_bar_right;
    // 右边的图片
    @BindView(R.id.iv_bar_right)
    ImageView iv_bar_right;
    // 右标题
    @BindView(R.id.tv_bar_right)
    TextView tv_bar_right;

    private final Context mContext;

    private String centerText;
    private String leftText;
    private String rightText;

    private int leftSrc;
    private int rightSrc;
    private boolean isShowRight;
    private boolean isShowLeft;
    private float centerTextSize;
    private int centerTextColor;

    private View mView;

    private OnNavigationBarClickListener barClickListener;

    public ZHINavigationBar(Context context) {
        this(context, null);
    }

    public ZHINavigationBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHINavigationBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CFNavigationBar);

        leftSrc = ta.getResourceId(R.styleable.CFNavigationBar_leftSrc, R.drawable.ic_left);
        rightSrc = ta.getResourceId(R.styleable.CFNavigationBar_rightSrc, R.drawable.ic_left);
        isShowRight = ta.getBoolean(R.styleable.CFNavigationBar_isShowRight, false);
        isShowLeft = ta.getBoolean(R.styleable.CFNavigationBar_isShowLeft, true);

        centerText = ta.getString(R.styleable.CFNavigationBar_centerText);
        leftText = ta.getString(R.styleable.CFNavigationBar_leftText);
        rightText = ta.getString(R.styleable.CFNavigationBar_rightText);

        centerTextSize = ta.getInteger(R.styleable.CFNavigationBar_centerTextSize, 18);
        centerTextColor = ta.getColor(R.styleable.CFNavigationBar_centerTextColor, getResources().getColor(R.color.white));

        ta.recycle();

        initContentView();
        initView();
        initListener();
        setDefaultListenner();
    }

    private void initContentView() {
        mView = View.inflate(mContext, R.layout.view_navigation_bar, this);
        ButterKnife.bind(this);
    }

    // 初始化页面，根据设置项显示
    private void initView() {

        if (TextUtils.isEmpty(centerText)) {
            centerText = "";
        }
        if (TextUtils.isEmpty(leftText)) {
            leftText = "";
        }
        if (TextUtils.isEmpty(rightText)) {
            rightText = "";
        }

        tv_bar_title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, centerTextSize);
        tv_bar_title.setTextColor(centerTextColor);
        tv_bar_title.setText(centerText);

        tv_bar_left.setText(leftText);
        tv_bar_right.setText(rightText);

        iv_bar_left.setVisibility(isShowLeft ? View.VISIBLE : View.GONE);
        tv_bar_left.setVisibility(isShowLeft ? View.VISIBLE : View.GONE);

        iv_bar_right.setVisibility(isShowRight ? View.VISIBLE : View.GONE);
        tv_bar_right.setVisibility(isShowRight ? View.VISIBLE : View.GONE);
    }

    private void initListener() {
        iv_bar_left.setOnClickListener(this);
        tv_bar_left.setOnClickListener(this);
        iv_bar_right.setOnClickListener(this);
        tv_bar_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_bar_left:
            case R.id.tv_bar_left:
                if (barClickListener != null) {
                    barClickListener.onNavigationBarLeftClick(v);
                }
                break;
            case R.id.iv_bar_right:
            case R.id.tv_bar_right:
                if (barClickListener != null) {
                    barClickListener.onNavigationBarRightClick(v);
                }
                break;
        }
    }

    public OnNavigationBarClickListener getBarClickListener() {
        return barClickListener;
    }

    public void setBarClickListener(OnNavigationBarClickListener barClickListener) {
        this.barClickListener = barClickListener;
    }

    public ZHINavigationBar setBarTextColor(int color) {
        tv_bar_title.setTextColor(color);
        return this;
    }

    public ZHINavigationBar setCenterText(String text) {
        tv_bar_title.setText(text);
        return this;
    }

    public ZHINavigationBar setCenterTextBold(){
        tv_bar_title.setTypeface(Typeface.DEFAULT_BOLD);
        return this;
    }

    public ZHINavigationBar setCenterTextSize(int fontSize){
        tv_bar_title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize);
        return this;
    }

    public ZHINavigationBar setLeftImgage(int leftImgage) {
        leftSrc = leftImgage;
        iv_bar_left.setImageResource(leftSrc);
        return this;
    }

    public ZHINavigationBar setRightImgage(int rightImgage) {
        rightSrc = rightImgage;
        iv_bar_right.setVisibility(VISIBLE);
        tv_bar_right.setVisibility(GONE);
        iv_bar_right.setImageResource(rightSrc);
        return this;
    }

    public ZHINavigationBar hideLeft() {
        iv_bar_left.setVisibility(View.GONE);
        tv_bar_left.setVisibility(View.GONE);
        return this;
    }

    public ZHINavigationBar showLeft() {
        iv_bar_left.setVisibility(View.VISIBLE);
        tv_bar_left.setVisibility(View.VISIBLE);
        return this;
    }

    public ZHINavigationBar hideRight() {
        tv_bar_right.setVisibility(View.GONE);
        iv_bar_right.setVisibility(View.GONE);
        return this;
    }

    public ZHINavigationBar showRight() {
        iv_bar_right.setVisibility(View.VISIBLE);
        tv_bar_right.setVisibility(View.VISIBLE);
        return this;
    }

    public ZHINavigationBar setLeftText(String text) {
        leftText = text;
        if (TextUtils.isEmpty(leftText)) {
            leftText = "";
        }

        iv_bar_left.setVisibility(View.GONE);
        tv_bar_left.setVisibility(VISIBLE);
        tv_bar_left.setText(leftText);

        return this;
    }

    public ZHINavigationBar setRightText(String text) {
        rightText = text;
        if (TextUtils.isEmpty(rightText)) {
            rightText = "";
        }

        iv_bar_right.setVisibility(GONE);
        tv_bar_right.setVisibility(VISIBLE);
        tv_bar_right.setText(rightText);

        return this;
    }

    public ImageView getRightImageView() {
        return iv_bar_right;
    }

    public ZHINavigationBar setDefaultListenner() {
        this.setBarClickListener(new OnNavigationBarClickListener() {
            @Override
            public void onNavigationBarLeftClick(View v) {
                if (mContext instanceof FragmentActivity) {
                    FragmentActivity activity = (FragmentActivity) mContext;
                    activity.onBackPressed();
                }
                if (mContext instanceof Activity) {
                    ((Activity) mContext).finish();
                }
            }

            @Override
            public void onNavigationBarRightClick(View v) {

            }
        });

        return this;
    }

    public interface OnNavigationBarClickListener {
        void onNavigationBarLeftClick(View v);

        void onNavigationBarRightClick(View v);
    }
}